//
//  CameraSystemViewController.m
//  MobileBank
//
//  Created by ylj on 2022/2/10.
//

#import "CameraSystemViewController.h"

@interface CameraSystemViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation CameraSystemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setCameraSystem];
}

- (void)setCameraSystem {
    
    // 创建UIImagePickerController实例
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    // 设置代理
    imagePickerController.delegate = self;
    // 是否显示裁剪框编辑（默认为NO），等于YES的时候，照片拍摄完成可以进行裁剪
    imagePickerController.allowsEditing = YES;
    // 设置照片来源为相机
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    // 设置进入相机时使用前置或后置摄像头
    imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    // 展示选取照片控制器
    [self presentViewController:imagePickerController animated:YES completion:nil];
}


#pragma mark - UIImagePickerControllerDelegate
// 完成图片的选取后调用的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // 选取完图片后跳转回原控制器
    [picker dismissViewControllerAnimated:NO completion:nil];
    /* 此处参数 info 是一个字典，下面是字典中的键值 （从相机获取的图片和相册获取的图片时，两者的info值不尽相同）
     * UIImagePickerControllerMediaType; // 媒体类型
     * UIImagePickerControllerOriginalImage; // 原始图片
     * UIImagePickerControllerEditedImage; // 裁剪后图片
     * UIImagePickerControllerCropRect; // 图片裁剪区域（CGRect）
     * UIImagePickerControllerMediaURL; // 媒体的URL
     * UIImagePickerControllerReferenceURL // 原件的URL
     * UIImagePickerControllerMediaMetadata // 当数据来源是相机时，此值才有效
     */
    // 从info中将图片取出，并加载到imageView当中
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [self successBlockImage:image];
}

// 取消选取调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:NO completion:^{
//        [self.navigationController popViewControllerAnimated:NO];
        [self cancelBlock:[UIImage alloc]];
    }];
}

- (void)successBlockImage:(UIImage *)image {
    
//    [self.navigationController popViewControllerAnimated:YES];
//    if (self.takePictureBlock) {
//        self.takePictureBlock(YES, image);
//    }
}

- (void)cancelBlock:(UIImage *)image {
//    if (self.takePictureBlock) {
//        self.takePictureBlock(NO, image);
//    }
}

@end
